<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace Model\MessageBundle\Model;
use Base\BaseModel;
use Model\MessageBundle\Setting\MessageSetting;

class MessageModel extends BaseModel{

    private $table = "Message";

    /*
     * 列表基本设置【通用模块必须开发】
     */
    public function listSettingDefault()
    {
        $settings = new MessageSetting($this->getDoctrine());
        return $settings->listConfig();
    }

    /*
     * 为后台获取所有文章分类【通用模块必须开发】
     */
    /**
     * @param array $params $param[textKey]=textKey , $param[conditions][table][field][eq|gt|lt...]=value , $param[orderBy][table][field]=DESC|ASC
     * @param int $offset
     * @param int $perPage
     * @return array
     */
    public function getAdminLists($params ,$offset = 0 , $perPage = 10)
    {
        $qb = $this->getQueryFromParam($params);
        $lists = $this->getAllResult($qb,$offset,$perPage);
        $total = $this->countAllResult($qb);

        return array(
            "total" => $total ? $total : 0,
            "data"  => $lists
        );
    }

    public function commAdd($params)
    {
        //设置默认值
        $defaults = array(
            "title"     => "消息标题被外星人抹去了！",
            "category"  => 1,
            "content"   => "",
            "addTime"   => time(),
            "isHot"     => 0,
            "isTop"     => 0,
            "isWebIndex"=> 0,
            "isAppIndex"=> 0,
            "fromWho"      => 0,
            "toWho"        => 0,
        );
        foreach ($defaults as $k => &$v) {
            if (isset($params[$k]) && $params[$k] !== "") {
                $v = $params[$k];
            }
        }
        $qb = $this->getQueryBuilder();
        if (isset($params["id"])) {
            //编辑
            $qb->update($this->table);
            $qb->where("id = :id")->setParameter("id",$params["id"]);
            foreach ($defaults as $key => $val) {
                $qb->set("$key",":$key")->setParameter(":$key",$val);
            }
        } else {
            //新增
            $qb->insert($this->table);
            $keys = array_keys($defaults);
            $values = array_values($defaults);
            $insertKeys = array();
            foreach ($keys as $k => $val) {
                $insertKeys[$val] = "?";
            }
            $qb->values($insertKeys)->setParameters($values);
        }
        $qb->execute();
        if ( $qb->getState() === 1) {
            return true;
        } else {
            return $qb->getState();
        }
    }

    public function getListForUser($userId,$page = 0,$perPage = 0)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("m.id","m.title","m.addTime","m.content","c.name");
        $qb->from("Message","m");
        $qb->leftJoin("m","MessageCategory","c","c.id = m.category");
        if ($page != 0 || $perPage != 0) {
            $qb->setFirstResult($page.$perPage);
            $qb->setMaxResults($perPage);
        }
        $qb->where("toWho = ?")->setParameter(0,$userId);
        $qb->orWhere("toWho = 0");
        $qb->orderBy("id","desc");
        return $qb->execute()->fetchAll();
    }
}